package com.lyq.pdf.pdfbox;

import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

/**
 *
 */
public class PDFReader {
    public static void main(String[] args) {
        PDFReader pdfReader = new PDFReader();
        pdfReader.readTextImage();
//        pdfReader.read1();
//        pdfReader.read2();
//        pdfReader.read3();

//        try {
//            PDDocument document = PDDocument.load(new File("D:\\入职体检单.pdf"));
//
//            int pages = document.getNumberOfPages();
//
//            PDFTextStripper pdfStripper = new PDFTextStripper();
//
//            pdfStripper.setStartPage(1);
//            pdfStripper.setEndPage(pages);
//            String text = pdfStripper.getText(document);
//            System.out.println(text);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    /**
     * 读取所有页，所有文本
     */
    public void read1() {
        try {
            File filePath = new File("D:\\入职体检单.pdf");
            //加载PDF文件
            PDDocument document = PDDocument.load(filePath);
            //文本剥离器
            PDFTextStripper stripper = new PDFTextStripper();
            //按位置进行排序
            stripper.setSortByPosition(true);
            //获取文本
            String text = stripper.getText(document);
            //关闭文档对象
            document.close();
            System.out.println(text);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 按页读取文本
     */
    public void read2() {
        try {
            File filePath = new File("D:\\入职体检单.pdf");
            //加载PDF文件
            PDDocument document = PDDocument.load(filePath);
            //文本剥离器
            PDFTextStripper stripper = new PDFTextStripper();
            //按页进行读取，页码从1开始
            stripper.setStartPage(1);
            stripper.setEndPage(1);
            //按位置进行排序
            stripper.setSortByPosition(true);
            //获取文本
            String text = stripper.getText(document);
            //关闭文档对象
            document.close();
            System.out.println(text);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 按坐标读取
     */
    public void read3() {
        try {
            File filePath = new File("D:\\入职体检单.pdf");
            //加载PDF文件
            PDDocument document = PDDocument.load(filePath);
            //按区域读取文本剥离器
            PDFTextStripperByArea stripper = new PDFTextStripperByArea();
            //新建区域，坐标：x,y；宽高：width，height
            Rectangle rectangle = new Rectangle(39, 39, 95, 38);
            //设置区域
            stripper.addRegion("regionName", rectangle);
            //按位置进行排序
            stripper.setSortByPosition(true);
            //获取目录
            PDDocumentCatalog catalog = document.getDocumentCatalog();
            //获取页码树
            PDPageTree tree = catalog.getPages();
            //获取指定页，从0开始
            PDPage page = tree.get(0);
            //提取页面信息
            stripper.extractRegions(page);
            //获取指定区域名称对应区域的文本
            String regionText = stripper.getTextForRegion("regionName");
            System.out.println(regionText);
            //关闭文档对象
            document.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 读取文本内容和图片
     *
     * @throws Exception
     */
    public void readTextImage() {
        try {
//            File file = new File("D:\\手动模式暂停继续两个学习中复现(副本).pdf");
            File file = new File("D://topic.pdf");
            PDDocument doc = PDDocument.load(file);
            PDFTextStripper textStripper = new PDFTextStripper();
            for (int i = 1; i <= doc.getNumberOfPages(); i++) {
                textStripper.setStartPage(i);
                textStripper.setEndPage(i);
                String s = textStripper.getText(doc);
                System.out.println("第 " + i + " 页 :" + s);
                // 读取图片
                PDPage page = doc.getPage(i - 1);
                PDResources resources = page.getResources();
                // 获取页中的对象
                Iterable<COSName> xobjects = resources.getXObjectNames();
                if (xobjects != null) {
                    Iterator<COSName> imageIter = xobjects.iterator();
                    while (imageIter.hasNext()) {
                        COSName cosName = imageIter.next();
                        boolean isImageXObject = resources.isImageXObject(cosName);
                        if (isImageXObject) {
                            // 获取每页资源的图片
                            PDImageXObject ixt = (PDImageXObject) resources.getXObject(cosName);
                            File outputfile = new File("第 " + (i) + " 页" + cosName.getName() + ".jpg");
                            ImageIO.write(ixt.getImage(), "jpg", outputfile);
                        }
                    }
                }
            }
            doc.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 区域解析
     *
     * @throws Exception
     */
    public static void readRectangle() throws Exception {
        String filePath = "F:\\test.pdf";
        File file = new File(filePath);
        PDDocument doc = PDDocument.load(file);
        // 这个四边形所在区域在 y轴向下为正，x轴向右为正。
        int x = 35;
        int y = 300;
        int width = 50;
        int height = 50;
        PDFTextStripperByArea stripper = new PDFTextStripperByArea();
        stripper.setSortByPosition(true);
        // 划定区域
        Rectangle2D rect = new Rectangle(x, y, width, height);
        stripper.addRegion("area", rect);
        PDPage page = doc.getPage(1);
        stripper.extractRegions(page);
        // 获取区域的text
        String data = stripper.getTextForRegion("area");
        data = data.trim();
        System.out.println(data);
        doc.close();
    }

}
